package cn.lixin.pay.service.impl;

import cn.lixin.basic.exception.BusinessException;
import cn.lixin.basic.service.impl.BaseServiceImpl;
import cn.lixin.order.service.IProductOrderService;
import cn.lixin.pay.domain.AlipayInfo;
import cn.lixin.pay.domain.PayBill;
import cn.lixin.pay.mapper.AlipayInfoMapper;
import cn.lixin.pay.mapper.PayBillMapper;
import cn.lixin.pay.service.IPayBillService;
import cn.lixin.pay.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    /**
     * 统一支付
     * @param billTemp
     * @return
     */
    @Override
    public String pay(PayBill billTemp) {
        /*
            校验
         */
        if (billTemp == null) {
            throw new BusinessException("支付单为空");
        }
        // 校验是否能从数据库查询到改信息
        PayBill bill = payBillMapper.loadById(billTemp.getId());
        if (bill == null) {
            throw new BusinessException("支付单为空");
        }
         //0 余额 1 支付宝 2 微信 3 银联
        switch (bill.getPayChannel().intValue()){// 支付类型
            case 1 ://支付宝
                AlipayInfo info = alipayInfoMapper.loadByShopId(bill.getShop_id());
                return AlipayUtils.pay(info,bill);
            case 2 ://微信

                break;
            case 3 ://... 3 银联

            default : //...0 余额
        }

        return null;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
